//
//  KLAutoTrackerManager.m
//  RKLFoundationFrame
//
//  Created by RKL on 2018/2/11.
//  Copyright © 2018年 AppFrameWorkDemo. All rights reserved.
//

#import "KLAutoTrackerManager.h"
#import "UIButton+KLAutoTracker.h"
#import "UIViewController+KLAutoTracker.h"

@implementation KLAutoTrackerManager

+ (instancetype)sharedInstance {
    static id _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

#pragma mark - public method
/**
 开始打点
 
 @param successBlock 成功回调
 @param debugBlock 调试模式回调
 */
- (void)startWithCompletionBlockWithSuccess:(KLAutoTrackerManagerBlock)successBlock
                                      debug:(KLAutoTrackerManagerBlock)debugBlock
{
    static dispatch_once_t once;
    dispatch_once(&once, ^ {
        [UIButton startTracker];
        [UIViewController startTracker];
    });
    
    self.successBlock = successBlock;
    self.debugBlock = debugBlock;
}

- (void)saveToFileWithFileContent:(NSDictionary *)fileContentDic
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSString * cachesDir = [paths objectAtIndex:0];
    
    NSString * filePath = [cachesDir stringByAppendingPathComponent:@"NoBurialPoint.txt"];
    
    NSMutableArray * tempArr = [NSMutableArray new];
    
    NSArray * fileArr = [NSArray arrayWithContentsOfFile:filePath];
    
    for (NSDictionary * dic in fileArr)
    {
        [tempArr addObject:dic];
    }
    
    [tempArr addObject:fileContentDic];
    
    if (![KLAutoTrackerManager sharedInstance].isDebug)
    {
        if([tempArr writeToFile:filePath atomically:YES])
        {
            NSLog(@"存入成功");
            if ([KLAutoTrackerManager sharedInstance].successBlock)
            {
                [KLAutoTrackerManager sharedInstance].successBlock(fileContentDic);
            }
        }
        else
        {
            NSLog(@"存入失败");
            if ([KLAutoTrackerManager sharedInstance].isDebug &&
                [KLAutoTrackerManager sharedInstance].debugBlock)
            {
                [KLAutoTrackerManager sharedInstance].debugBlock(fileContentDic);
            }
        }
    }
    else
    {
        if ([KLAutoTrackerManager sharedInstance].isDebug &&
            [KLAutoTrackerManager sharedInstance].debugBlock)
        {
            [KLAutoTrackerManager sharedInstance].debugBlock(fileContentDic);
        }
    }
}


- (void)clearFileContent
{
    NSFileManager * defauleManager = [NSFileManager defaultManager];
    
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSString * cachesDir = [paths objectAtIndex:0];
    
    NSString * filePath = [cachesDir stringByAppendingPathComponent:@"NoBurialPoint.txt"];
    
    [defauleManager removeItemAtPath:filePath error:nil];
    
    [defauleManager createFileAtPath:filePath contents:nil attributes:nil];
}

@end
